package com.easychat.controller;

import com.easychat.annotation.GlobalInterceptor;
import com.easychat.entity.po.AppUpdate;
import com.easychat.entity.query.AppUpdateQuery;
import com.easychat.entity.vo.PaginationResultVO;
import com.easychat.entity.vo.ResponseVO;
import com.easychat.service.AppUpdateService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @Author: yudaxian
 * @Description: TODO
 * @DateTime: 2024/8/16 17:27
 **/
@RestController
@RequestMapping("/admin")
public class AdminAppUpdateController extends ABaseController {

    @Resource
    private AppUpdateService appUpdateService;

    @GlobalInterceptor(checkAdmin = true)
    @RequestMapping("/loadUpdateList")
    public ResponseVO loadUpdateList(AppUpdateQuery query) {
        query.setOrderBy("version desc");
        PaginationResultVO<AppUpdate> resultVO = appUpdateService.findListByPage(query);
        return getSuccessResponseVO(resultVO);
    }

    @GlobalInterceptor(checkAdmin = true)
    @RequestMapping("/saveUpdate")
    public ResponseVO saveUpdate(Integer id,
                                 @NotEmpty String version,
                                 @NotEmpty String updateDesc,
                                 @NotNull Integer fileType,
                                 String outerLink,
                                 MultipartFile file
                                 ) {
        AppUpdate appUpdate = AppUpdate.builder()
                .id(id)
                .version(version)
                .updateDesc(updateDesc)
                .fileType(fileType)
                .outerLink(outerLink)
                .build();
        appUpdateService.saveUpdate(appUpdate, file);
        return getSuccessResponseVO(null);
    }

    @GlobalInterceptor(checkAdmin = true)
    @RequestMapping("/delUpdate")
    public ResponseVO delUpdate(@NotNull Integer id) {
        appUpdateService.deleteAppUpdateById(id);
        return getSuccessResponseVO(null);
    }

    @GlobalInterceptor(checkAdmin = true)
    @RequestMapping("/postUpdate")
    public ResponseVO postUpdate(@NotNull Integer id,
                                 @NotNull Integer status,
                                 String grayscaleUid) {
        appUpdateService.postUpdate(id, status, grayscaleUid);
        return getSuccessResponseVO(null);
    }
}
